package cn.tfyhyc.springcloud.controller;

import cn.tfyhyc.springcloud.service.SentinelServiceimpl;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author 头发又黑又长
 * @Date 2022/10/25 20:49
 * @email zwb15083976291@163.com
 */
@Slf4j
@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        return " test A";
    }

    @GetMapping("/testB")
    public String testB() {
        log.info(Thread.currentThread().getName() + " \t ............testB");
        return " test B";
    }


    @Resource
    private SentinelServiceimpl sentinelServiceimpl;


    @GetMapping("/testgetAllA")
    public String testAllA() {
        sentinelServiceimpl.getAll();
        return " test A";
    }


    @GetMapping("/testgetAllB")
    public String testAllB() {
        sentinelServiceimpl.getAll();
        return " test B";
    }


    // 熔断降级 RT
    @GetMapping("/testD")
    public String testD() {
        // 暂停1秒线程
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("test DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
        return "test DDDDDDD";
    }

    // 熔断降级 异常比例
    @GetMapping("/testE")
    public String testE() {
        log.info("testE 异常比例");
        int age = 1 / 0;
        return "test testEtestEtestEtestE";
    }


    // 熔断降级 异常数
    @GetMapping("/testF")
    public String testF() {
        log.info("testF 异常比例");
        int age = 1 / 0;
        return "test testEtestFtestFtestFtestF";
    }

}
